import time
import unittest
import utils
import requests

from api.login import LoginApi
from utils import DBUtil


class TestLogin(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = LoginApi()

    def setUp(self) -> None:
        self.session = requests.Session()
        # # 数据清理
        # sql1 = "DELETE FROM mb_member WHERE phone='13020210002';"
        # sql2 = "DELETE FROM mb_member_info WHERE member_name='13020210002';"
        # sql3 = "DELETE FROM mb_member_register_log WHERE member_name='13020210002';"
        # DBUtil.execute(sql1, sql2, sql3)

    def tearDown(self) -> None:
        self.session.close()

    # 登录成功-用户名和密码正确
    def test01_login_success(self):
        # 测试数据
        keywords = "13012345678"
        password = "test123"

        # 调用接口
        r = self.login_api.login(self.session, keywords, password)
        print("login data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, "登录成功")

    # 登录失败-密码错误
    def test02_login_pwd_error(self):
        # 测试数据
        keywords = "13012345678"
        password = "test666"
        right_pwd = "test123"

        # 调用接口
        # 登录失败-密码错误-1次
        r1 = self.login_api.login(self.session, keywords, password)
        print("login data1=", r1.json())
        utils.common_assert(self, r1, 200, 100, "密码错误1次,达到3次将锁定账户")

        # 登录失败-密码错误-2次
        r2 = self.login_api.login(self.session, keywords, password)
        print("login data2=", r2.json())
        utils.common_assert(self, r2, 200, 100, "密码错误2次,达到3次将锁定账户")

        # 登录失败-密码错误-3次
        r3 = self.login_api.login(self.session, keywords, password)
        print("login data3=", r3.json())
        utils.common_assert(self, r3, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")

        # 登录失败-密码错误-3次，1分钟之内，输入正确的密码登录
        r4 = self.login_api.login(self.session, keywords, right_pwd)
        print("login data4=", r4.json())
        utils.common_assert(self, r4, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")

        # 登录成功-密码错误3次-1分钟后，输入正确密码
        time.sleep(60)
        r5 = self.login_api.login(self.session, keywords, right_pwd)
        print("login data5=", r5.json())
        utils.common_assert(self, r5, 200, 200, "登录成功")
